package com.example.actionsignin.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.actionsignin.R;
import com.example.actionsignin.entity.FaceImageEntity;
import com.example.actionsignin.entity.UserEntity;
import com.example.actionsignin.http.HttpUtil;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by 旭 on 2017/7/27/027.
 */

public class MineFragment extends Fragment {

    private View view;
    private TextView mName;
    private TextView mNumber;
    private ImageView mHead;

    private static final String TAG = "MineFragment";

    private String mPath;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_mine,container,false);

        initView();
        getData();

        return view;
    }

    private void initView() {
        mName = (TextView) view.findViewById(R.id.fragment_mine_name);
        mNumber = (TextView) view.findViewById(R.id.fragment_mine_number);
        mHead = (ImageView) view.findViewById(R.id.fragment_mine_head);
    }

    private void getData()
    {
        HttpUtil.onSend().onGetUser()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<UserEntity>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull UserEntity userEntity) {
                        if (userEntity != null)
                        {
                            mName.setText(userEntity.getName());
                            mNumber.setText(userEntity.getTelephone());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

        HttpUtil.onSend().getFaceImage()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<FaceImageEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(FaceImageEntity s) {
                        Log.d(TAG, "onNext: " + s.getFace());
//                        mFacePath = FileUtil.WritePhoto(s.getFace());
                        mPath = s.getFace();
                        Glide.with(getContext()).load(mPath).into(mHead);
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.d(TAG, "onError: " + e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
